<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-6-8
 * Time: 16:29
 */

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Factor;
use app\common\model\Fenorder;
use app\common\model\News as NewsModel;
use app\common\model\Newscat;
use think\Db;

/**
 * 前台首页控制器
 * @package app\index\controller
 */
class News extends Home
{
    public $noNeedLogin = ['*'];


    /**
     * 服务隐私协议
     */
    public function agreement(){
        if($this->request->isPost()) {
            $type = $this->request->get('type');
            switch ($type){
                //服务协议
                case 1;
                    $cateId = 5;
                    break;
                //隐私协议
                case 2;
                    $cateId = 6;
                    break;
                //联系客服
                case 3;
                    $cateId = 7;
                    break;
                //关于我们
                case 4;
                    $cateId = 8;
                    break;
            }
            $data = Db::name('qt_news')->where('category_id',$cateId)->value('content');
            $this->result('请求成功',$data,200);
        }
    }


    /**
     * 获取新闻列表
     */

    public function lists(){
        if($this->request->isGet()){
            $getData= $this->request->get();
            if(empty($getData['page'])){
                $getData['page'] = 1;
            }
            $pageData = $this->request->get('limit');
            if(empty($pageData) || !isset($pageData)){
                $limit = 10;
            }else{
                $limit = $pageData;
            }
            //按名称搜索
            if(isset($getData['title'])){
                //$where['title'] = ['like','%'.$getData['title'].'%'];
                $where['status'] = 1;
                $newsData = NewsModel::where($where)->where('title','like','%'.$getData['title'].'%')->field('id,title,image,read,create_time')->order('id desc')->paginate($limit);
                foreach ($newsData as $key =>$value){
                    $imgUrl = get_img_path($value['image']);
                    $newsData[$key]['image'] = $imgUrl;
                }
                $this->result('查询成功',$newsData,200);
            }
            //正常展示
            $where['status'] = 1;
            $newsData = NewsModel::where($where)->whereNotIn('category_id','1,3')->field('id,title,image,read,create_time')->order('id desc')->paginate($limit);
            foreach ($newsData as $key =>$value){
                $imgUrl = get_img_path($value['image']);
                $newsData[$key]['image'] = $imgUrl;
            }
            $this->result('查询成功',$newsData,200);

            $this->result('请求参数有误','',204);

        }
    }

    /**
     * 新闻文章详情
     * @throws \think\Exception
     */
    public function detail(){
        if($this->request->isGet()) {
            $id = $this->request->get('id');
            if (empty($id)) {
                $this->result('请求参数有误','',204);
            }
            $newData = NewsModel::where(['id'=>$id,'status'=>1])->field('id,title,content,create_time,read')->find();
            if(empty($newData)){
                $this->result('文章已被删除','',204);
            }
            NewsModel::where(['id'=>$id,'status'=>1])->setInc('read');
            $this->result('查询成功',$newData,200);
        }
    }


}
